package cn.edu.nwafu.core.controller.admin;

import cn.edu.nwafu.common.exception.BusinessException;
import cn.edu.nwafu.common.result.R;
import cn.edu.nwafu.common.result.ResponseEnum;
import cn.edu.nwafu.core.pojo.entity.AircraftInfo;
import cn.edu.nwafu.core.service.AircraftInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@RestController
@Slf4j
@Api(tags = "航班管理")
@RequestMapping("/admin/core/aircraftInfo")
public class AdminAircraftInfoController {

    @Autowired
    private AircraftInfoService aircraftInfoService;

    @ApiOperation("获取航班列表")
    @GetMapping("/getList")
    public R getList() {

        List<AircraftInfo> list = aircraftInfoService.list();

        return R.ok().data("list", list);
    }

    @ApiOperation("Excel数据的批量导入")
    @PostMapping("/import")
    public R batchImport(
            @ApiParam(value = "Excel数据字典文件", required = true)
            @RequestParam("file") MultipartFile file
    ) {

        InputStream inputStream = null;

        try {
            inputStream = file.getInputStream();
            aircraftInfoService.importData(inputStream);

            return R.ok().message("文件上传成功");
        } catch (IOException e) {
            throw new BusinessException(ResponseEnum.UPLOAD_ERROR);
        }
    }

    @ApiOperation("新增航班信息")
    @PutMapping("/save")
    public R save(
            @ApiParam(value = "新增航班信息", required = true)
            @RequestBody AircraftInfo aircraftInfo
    ) {
        aircraftInfoService.save(aircraftInfo);

        return R.ok().message("新增用户成功");
    }

}


